/**************************************************************************
Step 1. Include necessary header files such that the stuff your test logic needs is declared. 
Don't forget gtest.h, which declares the testing framework.                                                   
**************************************************************************/
#include "gtest/gtest.h"
#include "include/pf_file.h"
#include<stdio.h>
#include<errno.h>
#include<fcntl.h>
#include<poll.h>
#include<stdlib.h>
#ifndef IOV_MAX
#define IOV_MAX 1024
#endif


/**************************************************************************
Step 2. Use test fixture to design your complex cases. 
You can add SetUp() to initialize the test data and 
TearDown() to clear the test data for common usage
Pls remember that when you using test fixture, need to use macro "TEST_F" instead of "TEST"                                                 
**************************************************************************/
class PfFileTest : public testing::Test 
{
    /* This SetUp() will be called before every TEST CASE which is marked by macro "TEST_F" */
    virtual void SetUp() 
    {
        file_1G="/tmp/1G.txt";
        file_6G="/tmp/6G.txt";
        file_empty="/tmp/empty.txt";
    }

    /* This TearDown will be called after every TEST CASE which is marked by macro "TEST_F" */
    virtual void TearDown() 
    {

    }
    public:
    const char* file_1G;
    const char* file_6G;
    const char* file_empty;

};



/***************************************************************************
Step 3. Use macro "TEST_F" to start your test case                                      
You can design your test case as the example
***************************************************************************/
TEST_F(PfFileTest, pf_fseek_nomal_test)
{
    /* Define the local variables */
    int64_t ret           = 0;
    

    /* initilize test data for this case */
	FILE *fg1;
	FILE *fg6;
	int64_t offset;
	int whence;
	fg1 = fopen(file_1G,"r");
    ASSERT_NE((void*)(NULL), fg1)<<"open 1G.txt failed\n";
	fg6 = fopen(file_6G,"r");
    ASSERT_NE((void*)(NULL), fg6)<<"open 6G.txt failed";
    

    /* Call your function for test */
	offset         = 0;
	whence         = SEEK_SET;
	ret            = pf_fseek(fg1,offset,whence);
    EXPECT_EQ(0, ret);

	offset         = 1000;
	whence         = SEEK_SET;
	ret            = pf_fseek(fg1,offset,whence);
    EXPECT_EQ(0, ret);

	offset         = 0;
	whence         = SEEK_CUR;
	ret            = pf_fseek(fg1,offset,whence);
    EXPECT_EQ(0, ret);

	offset         = 1000;
	whence         = SEEK_CUR;
	ret            = pf_fseek(fg1,offset,whence);
    EXPECT_EQ(0, ret);

	offset         = -1000;
	whence         = SEEK_END;
	ret = pf_fseek(fg1,offset,whence);
    EXPECT_EQ(0, ret);

	offset         = (int64_t)2*1024*1024*1024;
	whence         = SEEK_SET;
	ret            = pf_fseek(fg1,offset,whence);
    EXPECT_EQ(0, ret);

	offset         = 0;
	whence         = SEEK_SET;
	ret            = pf_fseek(fg6,offset,whence);
    EXPECT_EQ(0, ret)<<"1 pf_fseek_bigfile_test failed";

	offset         = (int64_t)5*1024*1024*1024;
	whence         = SEEK_SET;
	ret            = pf_fseek(fg6,offset,whence);
    EXPECT_EQ(0, ret)<<"2 pf_fseek_bigfile_test failed";

	offset         = 0;
	whence         = SEEK_CUR;
	ret            = pf_fseek(fg6,offset,whence);
    EXPECT_EQ(0, ret)<<"3 pf_fseek_bigfile_test failed";

	offset         = (int64_t)-4*1024*1024*1024;
	whence         = SEEK_CUR;
	ret            = pf_fseek(fg6,offset,whence);
    EXPECT_EQ(0, ret)<<"4 pf_fseek_bigfile_test failed";

	offset         = (int64_t)-5*1024*1024*1024;
	whence         = SEEK_END;
	ret            = pf_fseek(fg6,offset,whence);
    EXPECT_EQ(0, ret)<<"5 pf_fseek_bigfile_test failed";


	/* Clear the test data for this case */
	fclose(fg1);
	fclose(fg6);

}

TEST_F(PfFileTest, pf_fseek_abnormal_test)
{
    /* Define the local variables */
    int64_t ret           = 0;
    

    /* initilize test data for this case */
	int64_t offset;
	int whence;
	FILE *in;
	in = fopen(file_1G,"r");
    ASSERT_NE((void*)(NULL), in)<<"open file failed";
    

    /* Call your function for test */
	offset         = 0;
	whence         = -1;
	ret            = pf_fseek(in,offset,whence);
    EXPECT_GT(0, ret)<<"1 pf_fseek_abnormal_test";

	offset         = -1;
	whence         = SEEK_SET;
	ret            = pf_fseek(in,offset,whence);
    EXPECT_GT(0, ret)<<"2 pf_fseek_abnormal_test";

#ifndef VALGRIND_CHECK /* Death test core dump makes valgrind too many errors */
    //death test
	offset         = 0;
	whence         = SEEK_SET;
	EXPECT_DEATH(pf_fseek(NULL,offset,whence),"");
#endif /* #ifndef VALGRIND_CHECK */

	/* Clear the test data for this case */
	fclose(in);
}


TEST_F(PfFileTest, pf_seek_normal_test)
{
    /* Define the local variables */
    int64_t ret           = 0;

    /* initilize test data for this case */
	int64_t offset;
	int g1;
	int g6;
	int whence;
	g1 = open(file_1G,O_RDONLY);
    ASSERT_LT(0, g1)<<"open file failed";
	g6 = open(file_6G,O_RDONLY);
    ASSERT_LT(0, g6)<<"open file failed";
    

    /* Call your function for test */
	offset         = 0;
	whence         = SEEK_SET;
	ret            = pf_seek(g1,offset,whence);
    EXPECT_EQ(0, ret)<<"1 pf_seek_nomal_test:"<<ret;

	offset         = 1000;
	whence         = SEEK_SET;
	ret            = pf_seek(g1,offset,whence);
    EXPECT_EQ(0, ret)<<"2 pf_seek_nomal_test:"<<ret;

	offset         = 0;
	whence         = SEEK_CUR;
	ret            = pf_seek(g1,offset,whence);
    EXPECT_EQ(0, ret)<<"3 pf_seek_nomal_test:"<<ret;

	offset         = 1000;
	whence         = SEEK_CUR;
	ret            = pf_seek(g1,offset,whence);
    EXPECT_EQ(0, ret)<<"4 pf_seek_nomal_test:"<<ret;

	offset = -1000;
	whence = SEEK_END;
	ret    = pf_seek(g1,offset,whence);
    EXPECT_EQ(0, ret)<<"5 pf_seek_nomal_test:"<<ret;
	
	offset         = (int64_t)2*1024*1024*1024;
	whence         = SEEK_SET;
	ret            = pf_seek(g1,offset,whence);
    EXPECT_EQ(0, ret)<<"6 pf_seek_nomal_test:"<<ret;

    /* test 6G file */
	offset         = 0;
	whence         = SEEK_SET;
	ret            = pf_seek(g6,offset,whence);
    EXPECT_EQ(0, ret)<<"1 pf_seek_bigfile_test:"<<ret;

	offset         = (int64_t)5*1024*1024*1024;
	whence         = SEEK_SET;
	ret            = pf_seek(g6,offset,whence);
    EXPECT_EQ(0, ret)<<"2 pf_seek_bigfile_test:"<<ret;

	offset         = 0;
	whence         = SEEK_CUR;
	ret            = pf_seek(g6,offset,whence);
    EXPECT_EQ(0, ret)<<"3 pf_seek_bigfile_test:"<<ret;

	offset         = (int64_t)-4*1024*1024*1024;
	whence         = SEEK_CUR;
	ret            = pf_seek(g6,offset,whence);
    EXPECT_EQ(0, ret)<<"4 pf_seek_bigfile_test:"<<ret;

	offset         = (int64_t)-5*1024*1024*1024;
	whence         = SEEK_END;
	ret            = pf_seek(g6,offset,whence);
    EXPECT_EQ(0, ret)<<"5 pf_seek_bigfile_test:"<<ret;

	/* Clear the test data for this case */
	close(g1);
	close(g6);
}

TEST_F(PfFileTest, pf_seek_abnormal_test)
{
    /* Define the local variables */
    int64_t ret           = 0;

    /* initilize test data for this case */
	int64_t offset;
	int whence;
	int in = -1;
	in     = open(file_1G,O_RDONLY);
    ASSERT_LT(0, in)<<"open file failed";
    

    /* Call your function for test */
	offset         = 0;
	whence         = -1;
	ret            = pf_seek(in,offset,whence);
    EXPECT_GT(0, ret)<<"1 pf_seek_exception_test:"<<ret;

	offset         = -1;
	whence         = SEEK_SET;
	ret            = pf_seek(in,offset,whence);
    EXPECT_GT(0, ret)<<"2 pf_seek_exception_test:"<<ret;

	in             = -1;
	offset         = 0;
	whence         = SEEK_SET;
	ret            = pf_seek(in,offset,whence);
    EXPECT_GT(0, ret)<<"3 pf_seek_exception_test:"<<ret;

	/* Clear the test data for this case */
	close(in);
}


TEST_F(PfFileTest, pf_set_file_lock_normal_test)
{
    /* Define the local variables */
    int ret            = 0;

    /* initilize test data for this case */
	pf_file_lock_e lockmode;
    int status     = 0;
	int rwfd       = -1;
	int wfd        = -1;
	int rfd        = -1;
	int64_t offset = 0;
	int64_t len    = 0;
	rwfd           = open(file_1G,O_RDWR);
	wfd            = open(file_1G,O_WRONLY);
	rfd            = open(file_1G,O_RDONLY);
    ASSERT_LT(0, rwfd)<<"open RDWR file failed:"<<rwfd;
    ASSERT_LT(0, wfd)<<"open WRONLY file failed:"<<wfd;
    ASSERT_LT(0, rfd)<<"open RDONLY file failed:"<<rfd;

    /* Call your function for test */
	/* set write lock*/
	lockmode = PF_FILE_WLOCK;
	len      = 100;
	offset   = 0;

    errno = 0;
	ret   = pf_set_file_lock(rwfd,offset,len,lockmode);
    EXPECT_EQ(0,ret)<<"1:set file wlock failed:"<<ret;

    errno = 0;
	ret   = pf_set_file_lock(rwfd,offset,len,lockmode);
    EXPECT_EQ(0,ret)<<"2:set file wlock failed:"<<ret;

    if( fork() == 0)
    {

        errno    = 0;
        lockmode = PF_FILE_WLOCK;
        offset   = 0;
        ret      = pf_set_file_lock(wfd,offset,len,lockmode);
        EXPECT_GT(0,ret)<<"1:child set file wlock failed:"<<ret;

        errno  = 0;
        offset = 101;
        ret    = pf_set_file_lock(wfd,offset,len,lockmode);
        EXPECT_EQ(0,ret)<<"2:child set file wlock failed:"<<ret;

        errno  = 0;
        offset = 0;
        ret    = pf_set_file_lock(rfd,offset,len,lockmode);
        EXPECT_GT(0,ret)<<"3:child set file wlock failed:"<<ret;

        errno    = 0;
        lockmode = PF_FILE_RLOCK;
        offset   = 0;
        ret      = pf_set_file_lock(rfd,offset,len,lockmode);
        EXPECT_GT(0,ret)<<"1:child set file rlock failed:"<<ret;

        errno  = 0;
        offset = 101;
        ret    = pf_set_file_lock(rfd,offset,len,lockmode);
        EXPECT_EQ(0,ret)<<"2:child set file rlock failed:"<<ret;

        errno  = 0;
        offset = 0;
        ret    = pf_set_file_lock(wfd,offset,len,lockmode);
        EXPECT_GT(0,ret)<<"3:child set file rlock failed:"<<ret;

        errno    = 0;
        offset   = 0;
        lockmode = PF_FILE_UNLOCK;
        ret      = pf_set_file_lock(wfd,offset,len,lockmode);
        EXPECT_EQ(0,ret)<<"1:child set file unlock failed:"<<ret;

        close(rfd);
        close(wfd);
        close(rwfd);
        exit(0);
    }
    else
    {
        wait(&status); 
        offset   = 0;
        len      = 0;
        lockmode = PF_FILE_UNLOCK;
        ret      = pf_set_file_lock(wfd,offset,len,lockmode);
        EXPECT_EQ(0,ret)<<"1:set file unlock failed:"<<ret;
    }
    
    // set unlock
    lockmode = PF_FILE_UNLOCK;
    len      = 100;
    offset   = 0;
    ret      = pf_set_file_lock(rwfd,offset,len,lockmode);

    // set read lock
    lockmode = PF_FILE_RLOCK;
    len      = 100;
    offset   = 0;
    ret      = pf_set_file_lock(rwfd,offset,len,lockmode);
    EXPECT_EQ(0,ret)<<"1:set file rlock failed:"<<ret;

    if( fork() == 0)
    {
        close(rwfd);

        errno    = 0;
        lockmode = PF_FILE_WLOCK;
        ret      = pf_set_file_lock(wfd,offset,len,lockmode);
        EXPECT_GT(0,ret)<<"4:child set file wlock failed:"<<ret;

        errno  = 0;
        offset = 101;
        ret    = pf_set_file_lock(wfd,offset,len,lockmode);
        EXPECT_EQ(0,ret)<<"5:child set file wlock failed:"<<ret;

        errno    = 0;
        lockmode = PF_FILE_RLOCK;
        offset   = 0;
        ret      = pf_set_file_lock(rfd,offset,len,lockmode);
        EXPECT_EQ(0,ret)<<"4:child set file rlock failed:"<<ret;

        errno    = 0;
        offset   = 0;
        lockmode = PF_FILE_UNLOCK;
        ret      = pf_set_file_lock(wfd,offset,len,lockmode);
        EXPECT_EQ(0,ret)<<"2:child set file unlock failed:"<<ret;

        close(rfd);
        close(wfd);
        exit(0);
    }
    else
    {
        wait(&status); 
        offset   = 0;
        len      = 0;
        lockmode = PF_FILE_UNLOCK;
        ret      = pf_set_file_lock(wfd,offset,len,lockmode);
        EXPECT_EQ(0,ret)<<"3:set file unlock failed:"<<ret;
    }

    //set invalid lock
    offset   = 0;
    len      = 0;
    lockmode = PF_FILE_INVALID;
    ret      = pf_set_file_lock(wfd,offset,len,lockmode);
    EXPECT_GT(0,ret);

    /* Clear the test data for this case */
    close(rwfd);
    close(rfd);
    close(wfd);
}

TEST_F(PfFileTest, pf_get_file_lock_normal_test)
{
	pf_file_lock_e lockmode;
    int ret;
	int rwfd       = -1;
	int wfd        = -1;
	int rfd        = -1;
	int64_t offset = 0;
	int64_t len    = 0;
	rwfd           = open(file_1G,O_RDWR);
    ASSERT_LT(0, rwfd)<<"open RDWR file failed:"<<rwfd;
	len=100;
	offset =0;

    //wlock
	lockmode = PF_FILE_WLOCK;
    errno    = 0;
	ret      = pf_set_file_lock(rwfd,offset,len,lockmode);
    EXPECT_EQ(0,ret)<<ret<<" errno:"<<errno;

    errno = 0;
    ret   = pf_get_file_lock(rwfd,offset,0);
    EXPECT_EQ(PF_FILE_UNLOCK,ret)<<ret<<" errno:"<<errno;

    if( fork() == 0)
    {
        wfd = open(file_1G,O_WRONLY);
        ASSERT_LT(0, wfd)<<"open WRONLY file failed:"<<wfd;

        errno = 0;
        ret   = pf_get_file_lock(wfd,offset,0);
        EXPECT_EQ(PF_FILE_WLOCK,ret)<<ret<<" errno:"<<errno;


        close(rwfd);
        close(rfd);
        close(wfd);
        exit(0);
    }
    else
    {
        wait(NULL); 
    }

	lockmode = PF_FILE_UNLOCK;
    errno    = 0;
	ret      = pf_set_file_lock(rwfd,offset,len,lockmode);
    EXPECT_EQ(0,ret)<<ret<<" errno:"<<errno;

    errno = 0;
    ret   = pf_get_file_lock(rwfd,offset,0);
    EXPECT_EQ(PF_FILE_UNLOCK,ret)<<ret<<" errno:"<<errno;

    if( fork() == 0)
    {
        rfd = open(file_1G,O_RDONLY);
        ASSERT_LT(0, rfd)<<"open RDONLY file failed:"<<rfd;
        wfd = open(file_1G,O_WRONLY);
        ASSERT_LT(0, wfd)<<"open WRONLY file failed:"<<wfd;

        errno = 0;
        ret   = pf_get_file_lock(wfd,offset,0);
        EXPECT_EQ(PF_FILE_UNLOCK,ret)<<ret<<" errno:"<<errno;

        errno = 0;
        ret   = pf_get_file_lock(rfd,offset,0);
        EXPECT_EQ(PF_FILE_UNLOCK,ret)<<ret<<" errno:"<<errno;

        close(rwfd);
        close(rfd);
        close(wfd);
        exit(0);

    }
    else
    {
        wait(NULL); 
    }

	lockmode = PF_FILE_RLOCK;
    errno    = 0;
	ret      = pf_set_file_lock(rwfd,offset,len,lockmode);
    EXPECT_EQ(0,ret)<<ret<<" errno:"<<errno;

    errno = 0;
    ret   = pf_get_file_lock(rwfd,offset,0);
    EXPECT_EQ(PF_FILE_UNLOCK,ret)<<ret<<" errno:"<<errno;

    if( fork() == 0)
    {
        rfd = open(file_1G,O_RDONLY);
        ASSERT_LT(0, rfd)<<"open RDONLY file failed:"<<rfd;

        errno = 0;
        ret   = pf_get_file_lock(rfd,offset,0);
        EXPECT_EQ(PF_FILE_RLOCK,ret)<<ret<<" errno:"<<errno;

        close(rwfd);
        close(rfd);
        exit(0);

    }
    else
    {
        wait(NULL); 
    }

    //invalid offset
    errno = 0;
    ret   = pf_get_file_lock(rwfd,-1,0);
    EXPECT_EQ(PF_FILE_INVALID,ret)<<ret<<" errno:"<<errno;

    close(rwfd);
}

TEST_F(PfFileTest, pf_set_nonblock_normal)
{
    int ret;
    int fd;
	fd = open(file_1G,O_RDWR);
    ASSERT_LT(0,fd)<<"open 1G.txt failed\n";

    ret = pf_set_nonblock(fd,PF_ON);
    EXPECT_EQ(0,ret);
    
    ret = pf_set_nonblock(fd,PF_OFF);
    EXPECT_EQ(0,ret);
    
    ret = pf_set_nonblock(fd,PF_INVALID);
    EXPECT_GT(0,ret);
    
    ret = pf_set_nonblock(-1,PF_ON);
    EXPECT_GT(0,ret);

    close(fd);

}
TEST_F(PfFileTest, pf_ftell_normal)
{
	FILE *in;
    int64_t length;
    int64_t ret;
	in = fopen(file_6G,"r");
    ASSERT_NE((void*)(NULL), in)<<"open 6G.txt failed\n";

    length = pf_ftell(in);
    EXPECT_EQ((int64_t)0,length);
    
    ret = pf_fseek(in,0,SEEK_END);
    EXPECT_EQ((int64_t)0,length);

    length = pf_ftell(in);
    EXPECT_EQ((int64_t)6291456000LL,length);


#ifndef VALGRIND_CHECK /* Death test core dump makes valgrind too many errors */
    EXPECT_DEATH(pf_ftell(NULL),"");
#endif /* #ifndef VALGRIND_CHECK */
    fclose(in);
}

TEST_F(PfFileTest, pf_tell_normal)
{
	int in;
    int64_t length;
    int64_t ret;
	in = open(file_6G,O_RDWR);
    ASSERT_LT(0, in)<<"open 6G.txt failed\n";

    length = pf_tell(in);
    EXPECT_EQ((int64_t)0,length);
    
    ret = pf_seek(in,0,SEEK_END);
    EXPECT_EQ((int64_t)0,length);

    length = pf_tell(in);
    EXPECT_EQ((int64_t)6291456000LL,length);

    length = pf_tell(-1);
    EXPECT_GT((int64_t)0,length);

    close(in);
}
TEST_F(PfFileTest, pf_wait_fd_normal)
{
    /* initilize test data for this case */
    int ret;	
    short events;
    int32_t timeout;
    int fd = open(file_1G,O_RDWR);
    ASSERT_LT(0, fd)<<"open RDWR file failed:"<<fd;


    /* Call your function for test */
    ret     = 0;
    timeout = 2*1000;
    events  = POLLIN;
    ret     = pf_wait_fd(fd,events,timeout);
    EXPECT_LT(0,ret);

    ret     = 0;
    timeout = 2*1000;
    events  = POLLRDNORM;
    ret     = pf_wait_fd(fd,events,timeout);
    EXPECT_LT(0,ret);

    ret     = 0;
    timeout = 2*1000;
    events  = POLLRDBAND;
    ret     = pf_wait_fd(fd,events,timeout);
    EXPECT_EQ(0,ret);

    ret     = 0;
    timeout = 2*1000;
    events  = POLLPRI;
    ret     = pf_wait_fd(fd,events,timeout);
    EXPECT_EQ(0,ret);

    ret     = 0;
    timeout = 2*1000;
    events  = POLLOUT;
    ret     = pf_wait_fd(fd,events,timeout);
    EXPECT_LT(0,ret);

    ret     = 0;
    timeout = 2*1000;
    events  = POLLWRNORM;
    ret     = pf_wait_fd(fd,events,timeout);
    EXPECT_LT(0,ret);

    ret     = 0;
    timeout = 2*1000;
    events  = POLLWRBAND;
    ret     = pf_wait_fd(fd,events,timeout);
    EXPECT_EQ(0,ret);

    ret     = 0;
    timeout = 2*1000;
    events  = POLLIN|POLLOUT;
    ret     = pf_wait_fd(fd,events,timeout);
    EXPECT_LT(0,ret);

    ret     = 0;
    timeout = 0;
    events  = POLLIN|POLLOUT;
    ret     = pf_wait_fd(fd,events,timeout);
    EXPECT_LT(0,ret);

    ret     = 0;
    timeout = -1;
    events  = POLLIN|POLLOUT;
    ret     = pf_wait_fd(fd,events,timeout);
    EXPECT_LT(0,ret);
    
    ret     = 0;
    timeout = 2*1000;
    events  = POLLIN|POLLOUT;
    ret     = pf_wait_fd(-1,events,timeout);
    EXPECT_EQ(0,ret);


    /* Clear the test data for this case */
    close(fd);

}

TEST_F(PfFileTest, pf_read_n_normal)
{
    /* Define the local variables */
    int32_t ret = 0;

    /* initilize test data for this case */
    pf_fd_block_e block_type;
    char buf[1024];
    int32_t size = 1024;
    int32_t timeout;
    int nbfd = open(file_1G,O_RDWR|O_NONBLOCK);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;
    int bfd = open(file_1G,O_RDWR);
    ASSERT_LT(0, bfd)<<"open RDWR file failed:"<<nbfd;
    int enbfd = open(file_empty,O_RDWR|O_NONBLOCK);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;
    int ebfd = open(file_empty,O_RDWR);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;

    memset(buf,1,1024);

    /* Call your function for test */

    block_type = PF_FD_NONBLOCK;
    timeout    = 2*1000;
    ret        = pf_read_n(nbfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"1 pf_read_n_normal:"<<ret;

    block_type = PF_FD_BLOCK;
    timeout    = 2*1000;
    ret        = pf_read_n(bfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"2 pf_read_n_normal:"<<ret<<":errno"<<errno;

    block_type = PF_FD_UNKNOWN;//0
    timeout    = 2*1000;
    ret        = pf_read_n(nbfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"3 pf_read_n_normal:"<<ret;

    block_type = PF_FD_NONBLOCK;
    timeout    = 0;
    ret        = pf_read_n(nbfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"4 pf_read_n_normal:"<<ret;

    block_type = PF_FD_BLOCK;
    timeout    = 0;
    ret        = pf_read_n(bfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"5 pf_read_n_normal:"<<ret;

    block_type = PF_FD_NONBLOCK;
    timeout    = -1;
    ret        = pf_read_n(nbfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"6 pf_read_n_normal:"<<ret;

    block_type = PF_FD_BLOCK;
    timeout    = -1;
    ret        = pf_read_n(bfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"7 pf_read_n_normal:"<<ret;

    block_type = PF_FD_NONBLOCK;
    timeout    = -1;
    ret        = pf_read_n(enbfd, block_type, buf, size, timeout);
    EXPECT_EQ(0,ret)<<"8 pf_read_n_normal:"<<ret;

    block_type = PF_FD_BLOCK;
    timeout    = -1;
    ret        = pf_read_n(ebfd, block_type, buf, size, timeout);
    EXPECT_EQ(0,ret)<<"9 pf_read_n_normal:"<<ret;

	ret = pf_seek(bfd,-512,SEEK_END);
    EXPECT_EQ(0,ret);
	ret = pf_seek(nbfd,-512,SEEK_END);
    EXPECT_EQ(0,ret);

    block_type = PF_FD_NONBLOCK;
    timeout    = -1;
    ret        = pf_read_n(nbfd, block_type, buf, size, timeout);
    EXPECT_EQ(512,ret)<<"10 pf_read_n_normal:"<<ret;

    block_type = PF_FD_BLOCK;
    timeout    = -1;
    ret        = pf_read_n(bfd, block_type, buf, size, timeout);
    EXPECT_EQ(512,ret)<<"10 pf_read_n_normal:"<<ret;

    /* Clear the test data for this case */
    close(nbfd);
    close(bfd);
}



TEST_F(PfFileTest, pf_read_n_abnormal)
{
    /* Define the local variables */
    int ret = -1;

    /* initilize test data for this case */
    pf_fd_block_e block_type = PF_FD_BLOCK;
    char buf[1024];
    int32_t size    = 1024;
    int32_t timeout = 2*1000;
    int fd          = open(file_1G,O_RDWR);
    ASSERT_LT(0, fd)<<"open RDWR file failed:"<<fd;
    int nbfd        = open(file_1G,O_RDWR|O_NONBLOCK);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;

    memset(buf,1,1024);


    /* Call your function for test */

    block_type = PF_FD_BLOCK;
    size       = 1024;
    ret        = pf_read_n(-1, block_type, buf, size, timeout);
    EXPECT_GT(0,ret)<<"1 pf_write_n_abnormal:"<<ret;

    block_type = PF_FD_NONBLOCK;
    size       = 1024;
    ret        = pf_read_n(-1, block_type, buf, size, timeout);
    EXPECT_GT(0,ret)<<"2 pf_write_n_abnormal:"<<ret;

    block_type = PF_FD_INVALID; //-1
    timeout    = 2*1000;
    ret        = pf_read_n(fd, block_type, buf, size, timeout);
    EXPECT_EQ(-1,ret)<<"3 pf_write_n_abnormal:"<<ret;
    /*
       block_type = 100;
       timeout    = 2*1000;
       ret        = pf_read_n(fd, block_type, buf, size, timeout);
       EXPECT_EQ(-1,ret)<<"4 pf_write_n_abnormal:"<<ret;
       */

    block_type = PF_FD_BLOCK;
    size       = -1;
    ret        = pf_read_n(fd, block_type, buf, size, timeout);
    EXPECT_GT(0,ret)<<"6 pf_write_n_abnormal:"<<ret;

    block_type = PF_FD_NONBLOCK;
    size       = -1;
    ret        = pf_read_n(nbfd, block_type, buf, size, timeout);
    EXPECT_GT(0,ret)<<"7 pf_write_n_abnormal:"<<ret;

#ifndef VALGRIND_CHECK /* Death test core dump makes valgrind too many errors */
    //death test
    size       = 1024;
    timeout    = 2*1000;
    block_type = PF_FD_NONBLOCK;
    EXPECT_DEATH(pf_read_n(nbfd, block_type, NULL, size, timeout),"");
    block_type = PF_FD_BLOCK;
    EXPECT_DEATH(pf_read_n(fd, block_type, NULL, size, timeout),"");
#endif /* #ifndef VALGRIND_CHECK */

    /* Clear the test data for this case */
    close(fd);
    close(nbfd);
}

TEST_F(PfFileTest, pf_write_n_normal_test)
{


    /* Define the local variables */
    int32_t ret = 0;

    /* initilize test data for this case */
    pf_fd_block_e block_type;
    char buf[1024];
    int32_t size = 1024;
    int32_t timeout;
    int nbfd = open(file_1G,O_RDWR|O_NONBLOCK);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;
    int bfd = open(file_1G,O_RDWR);
    ASSERT_LT(0, bfd)<<"open RDWR file failed:"<<nbfd;

    memset(buf,1,1024);

    /* Call your function for test */

    block_type = PF_FD_NONBLOCK;
    timeout    = 2*1000;
    ret        = pf_write_n(nbfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"1 pf_write_n_normal:"<<ret;

    block_type = PF_FD_BLOCK;
    timeout    = 2*1000;
    ret        = pf_write_n(bfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"2 pf_write_n_normal:"<<ret<<":errno"<<errno;

    block_type = PF_FD_UNKNOWN;//0
    timeout    = 2*1000;
    ret        = pf_write_n(nbfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"3 pf_write_n_normal unblock:"<<ret;

    block_type = PF_FD_UNKNOWN;//0
    timeout    = 2*1000;
    ret        = pf_write_n(bfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"3 pf_write_n_normal block:"<<ret;

    block_type = PF_FD_NONBLOCK;
    timeout    = 2*1000;
    ret        = pf_write_n(nbfd, block_type, buf, 0, timeout);
    EXPECT_EQ(0,ret)<<"4 pf_write_n_normal:"<<ret;

    block_type = PF_FD_BLOCK;
    timeout    = 2*1000;
    ret        = pf_write_n(bfd, block_type, buf, 0, timeout);
    EXPECT_EQ(0,ret)<<"5 pf_write_n_normal:"<<ret;

    block_type = PF_FD_NONBLOCK;
    timeout    = 0;
    ret        = pf_write_n(nbfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"6 pf_write_n_normal:"<<ret;

    block_type = PF_FD_BLOCK;
    timeout    = 0;
    ret        = pf_write_n(bfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"7 pf_write_n_normal:"<<ret;

    block_type = PF_FD_NONBLOCK;
    timeout    = -1;
    ret        = pf_write_n(nbfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"8 pf_write_n_normal:"<<ret;

    block_type = PF_FD_BLOCK;
    timeout    = -1;
    ret        = pf_write_n(bfd, block_type, buf, size, timeout);
    EXPECT_EQ(size,ret)<<"9 pf_write_n_normal:"<<ret;



    /* Clear the test data for this case */
    close(nbfd);
    close(bfd);
}

TEST_F(PfFileTest, pf_write_n_abnormal_test)
{
    /* Define the local variables */
    int ret = -1;

    /* initilize test data for this case */
    pf_fd_block_e block_type = PF_FD_BLOCK;
    char buf[1024];
    int32_t size    = 1024;
    int32_t timeout = 2*1000;
    int fd          = open(file_1G,O_RDWR);
    ASSERT_LT(0, fd)<<"open RDWR file failed:"<<fd;
    int nbfd        = open(file_1G,O_RDWR|O_NONBLOCK);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;

    memset(buf,1,1024);

    /* Call your function for test */

    block_type = PF_FD_BLOCK;
    size       = 1024;
    ret        = pf_write_n(-1, block_type, buf, size, timeout);
    EXPECT_GT(0,ret)<<"1 pf_write_n_abnormal:"<<ret;

    block_type = PF_FD_NONBLOCK;
    size       = 1024;
    ret        = pf_write_n(-1, block_type, buf, size, timeout);
    EXPECT_GT(0,ret)<<"2 pf_write_n_abnormal:"<<ret;

    block_type = PF_FD_INVALID; //-1
    timeout    = 2*1000;
    ret        = pf_write_n(fd, block_type, buf, size, timeout);
    EXPECT_EQ(-1,ret)<<"3 pf_write_n_abnormal:"<<ret;
    /*
       block_type = 100;
       timeout    = 2*1000;
       ret        = pf_write_n(fd, block_type, buf, size, timeout);
       EXPECT_EQ(-1,ret)<<"4 pf_write_n_abnormal:"<<ret;
       */

    block_type = PF_FD_BLOCK;
    size       = -1;
    ret        = pf_write_n(fd, block_type, buf, size, timeout);
    EXPECT_GT(0,ret)<<"6 pf_write_n_abnormal:"<<ret;

    block_type = PF_FD_NONBLOCK;
    size       = -1;
    ret        = pf_write_n(nbfd, block_type, buf, size, timeout);
    EXPECT_GT(0,ret)<<"7 pf_write_n_abnormal:"<<ret;

#ifndef VALGRIND_CHECK /* Death test core dump makes valgrind too many errors */
    //death test
    block_type = PF_FD_BLOCK;
    EXPECT_DEATH(pf_write_n(fd, block_type, NULL, size, timeout),"");

    block_type = PF_FD_NONBLOCK;
    EXPECT_DEATH(pf_write_n(nbfd, block_type, NULL, size, timeout),"");
#endif /* #ifndef VALGRIND_CHECK */

    /* Clear the test data for this case */
    close(fd);
    close(nbfd);
}


TEST_F(PfFileTest, pf_readv_n_normal_test)
{
    /* Define the local variables */
    int32_t ret = -1;

    /* initilize test data for this case */
    pf_fd_block_e block_type;
    struct iovec iov[3];
    char buf1[128];
    char buf2[256];
    char buf3[512];
    iov[0].iov_base       = buf1;
    iov[0].iov_len        = 128;
    iov[1].iov_base       = buf2;
    iov[1].iov_len        = 256;
    iov[2].iov_base       = buf3;
    iov[2].iov_len        = 512;
    //int32_t total_size1 = 128;
    int32_t total_size2   = 128+256;
    int32_t total_size3   = 128+256+512;
    int iovcnt;
    int32_t timeout;
    int bfd = open(file_1G,O_RDWR);
    ASSERT_LT(0, bfd)<<"open RDWR file failed:"<<bfd;
    int nbfd = open(file_1G,O_RDWR|O_NONBLOCK);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;
    int enbfd = open(file_empty,O_RDWR|O_NONBLOCK);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;
    int ebfd = open(file_empty,O_RDWR);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;

    memset(buf1,1,128);
    memset(buf2,1,256);
    memset(buf3,1,512);
    /* Call your function for test */

    block_type = PF_FD_BLOCK;
    iovcnt     = 3;
    timeout    = 2*1000;
    ret        = pf_readv_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size3, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 3;
    timeout    = 2*1000;
    ret        = pf_readv_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size3, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 2;
    timeout    = 2*1000;
    ret        = pf_readv_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 2;
    timeout    = 2*1000;
    ret        = pf_readv_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_UNKNOWN;
    iovcnt     = 2;
    timeout    = 2*1000;
    ret        = pf_readv_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 2;
    timeout    = 0;
    ret        = pf_readv_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 2;
    timeout    = 0;
    ret        = pf_readv_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 2;
    timeout    = -1;
    ret        = pf_readv_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 2;
    timeout    = -1;
    ret        = pf_readv_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 2;
    timeout    = -1;
    ret        = pf_readv_n(ebfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(0, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 2;
    timeout    = -1;
    ret        = pf_readv_n(enbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(0, ret);

	ret = pf_seek(bfd,-128,SEEK_END);
    EXPECT_EQ(0,ret);
	ret = pf_seek(nbfd,-128,SEEK_END);
    EXPECT_EQ(0,ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 2;
    timeout    = -1;
    ret        = pf_readv_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(128, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 2;
    timeout    = -1;
    ret        = pf_readv_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(128, ret);

    /* Clear the test data for this case */
    close(nbfd);
    close(bfd);
    close(enbfd);
    close(ebfd);
}

TEST_F(PfFileTest, pf_readv_n_abnormal_test)
{
    /* Define the local variables */
    int32_t ret = -1;

    /* initilize test data for this case */
    pf_fd_block_e block_type;
    struct iovec iov[3];
    char buf1[128];
    char buf2[256];
    char buf3[512];
    iov[0].iov_base = buf1;
    iov[0].iov_len  = 128;
    iov[1].iov_base = buf2;
    iov[1].iov_len  = 256;
    iov[2].iov_base = buf3;
    iov[2].iov_len  = 512;
    int iovcnt;
    int32_t timeout;
    int bfd = open(file_1G,O_RDWR);
    ASSERT_LT(0, bfd)<<"open RDWR file failed:"<<bfd;
    int nbfd = open(file_1G,O_RDWR|O_NONBLOCK);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;

    memset(buf1,1,128);
    memset(buf2,1,256);
    memset(buf3,1,512);
    /* Call your function for test */

    block_type = PF_FD_BLOCK;
    iovcnt     = 3;
    timeout    = 2*1000;
    ret        = pf_readv_n(-1, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 3;
    timeout    = 2*1000;
    ret        = pf_readv_n(-1, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

    block_type = PF_FD_INVALID;
    iovcnt     = 3;
    timeout    = 2*1000;
    ret        = pf_readv_n(-1, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);
    /*
       block_type = 100;
       iovcnt     = 3;
       timeout    = 2*1000;
       ret = pf_readv_n(-1, block_type, iov, iovcnt, timeout);
       EXPECT_GT(0, ret);
       */

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 0;
    timeout    = 2*1000;
    ret        = pf_readv_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 0;
    timeout    = 2*1000;
    ret        = pf_readv_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = -1;
    timeout    = 2*1000;
    ret        = pf_readv_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = -1;
    timeout    = 2*1000;
    ret        = pf_readv_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

/*
    block_type = PF_FD_NONBLOCK;
    iovcnt     = 10;
    timeout    = 2*1000;
    ret        = pf_readv_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 10;
    timeout    = 2*1000;
    ret        = pf_readv_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);
    */

    block_type = PF_FD_BLOCK;
    iovcnt     = int(IOV_MAX+1);
    timeout    = 2*1000;
    ret        = pf_readv_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

#ifndef VALGRIND_CHECK /* Death test core dump makes valgrind too many errors */
    //death test
    block_type = PF_FD_BLOCK;
    EXPECT_DEATH(pf_readv_n(bfd, block_type, NULL, iovcnt, timeout),"");

    block_type = PF_FD_NONBLOCK;
    EXPECT_DEATH(pf_readv_n(nbfd, block_type, NULL, iovcnt, timeout),"");
#endif /* #ifndef VALGRIND_CHECK */

    /* Clear the test data for this case */
    close(bfd);
    close(nbfd);
}


TEST_F(PfFileTest, pf_writev_n_normal)
{
    /* Define the local variables */
    int32_t ret = -1;

    /* initilize test data for this case */
    pf_fd_block_e block_type;
    struct iovec iov[3];
    char buf1[128];
    char buf2[256];
    char buf3[512];
    iov[0].iov_base       = buf1;
    iov[0].iov_len        = 128;
    iov[1].iov_base       = buf2;
    iov[1].iov_len        = 256;
    iov[2].iov_base       = buf3;
    iov[2].iov_len        = 512;
    //int32_t total_size1 = 128;
    int32_t total_size2   = 128+256;
    int32_t total_size3   = 128+256+512;
    int iovcnt;
    int32_t timeout;
    int bfd               = open(file_1G,O_RDWR);
    ASSERT_LT(0, bfd)<<"open RDWR file failed:"<<bfd;
    int nbfd              = open(file_1G,O_RDWR|O_NONBLOCK);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;

    memset(buf1,1,128);
    memset(buf2,1,256);
    memset(buf3,1,512);
    /* Call your function for test */

    block_type = PF_FD_BLOCK;
    iovcnt     = 3;
    timeout    = 2*1000;
    ret        = pf_writev_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size3, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 3;
    timeout    = 2*1000;
    ret        = pf_writev_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size3, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 2;
    timeout    = 2*1000;
    ret        = pf_writev_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 2;
    timeout    = 2*1000;
    ret        = pf_writev_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_UNKNOWN;
    iovcnt     = 2;
    timeout    = 2*1000;
    ret        = pf_writev_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 2;
    timeout    = 0;
    ret        = pf_writev_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 2;
    timeout    = 0;
    ret        = pf_writev_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 2;
    timeout    = -1;
    ret        = pf_writev_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 2;
    timeout    = -1;
    ret        = pf_writev_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(total_size2, ret);

    //write 0 byte
    iov[0].iov_len = 0;
    iov[1].iov_len = 0;
    iov[2].iov_len = 0;

    block_type     = PF_FD_BLOCK;
    iovcnt         = 2;
    timeout        = -1;
    ret            = pf_writev_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(0, ret);

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 2;
    timeout    = -1;
    ret        = pf_writev_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_EQ(0, ret);

    /* Clear the test data for this case */
    close(nbfd);
    close(bfd);
}

TEST_F(PfFileTest, pf_writev_n_abnormal)
{

    /* Define the local variables */
    int32_t ret = -1;

    /* initilize test data for this case */
    pf_fd_block_e block_type;
    struct iovec iov[3];
    char buf1[128];
    char buf2[256];
    char buf3[512];
    iov[0].iov_base = buf1;
    iov[0].iov_len  = 128;
    iov[1].iov_base = buf2;
    iov[1].iov_len  = 256;
    iov[2].iov_base = buf3;
    iov[2].iov_len  = 512;
    int iovcnt;
    int32_t timeout;
    int bfd         = open(file_1G,O_RDWR);
    ASSERT_LT(0, bfd)<<"open RDWR file failed:"<<bfd;
    int nbfd        = open(file_1G,O_RDWR|O_NONBLOCK);
    ASSERT_LT(0, nbfd)<<"open RDWR file failed:"<<nbfd;

    memset(buf1,1,128);
    memset(buf2,1,256);
    memset(buf3,1,512);

    /* Call your function for test */

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 3;
    timeout    = 2*1000;
    ret        = pf_writev_n(-1, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 3;
    timeout    = 2*1000;
    ret        = pf_writev_n(-1, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

    block_type = PF_FD_INVALID;
    iovcnt     = 3;
    timeout    = 2*1000;
    ret        = pf_writev_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);
    /*
       block_type = 100;
       iovcnt     = 3;
       timeout    = 2*1000;
       ret = pf_writev_n(nbfd, block_type, iov, iovcnt, timeout);
       EXPECT_GT(0, ret);
       */

    block_type = PF_FD_NONBLOCK;
    iovcnt     = 0;
    timeout    = 2*1000;
    ret        = pf_writev_n(nbfd, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

    block_type = PF_FD_BLOCK;
    iovcnt     = 0;
    timeout    = 2*1000;
    ret        = pf_writev_n(bfd, block_type, iov, iovcnt, timeout);
    EXPECT_GT(0, ret);

#ifndef VALGRIND_CHECK /* Death test core dump makes valgrind too many errors */
    //death test
    block_type = PF_FD_NONBLOCK;
    EXPECT_DEATH(pf_write_n(nbfd, block_type, NULL, iovcnt, timeout),"");

    block_type = PF_FD_BLOCK;
    EXPECT_DEATH(pf_write_n(bfd, block_type, NULL, iovcnt, timeout),"");
#endif /* #ifndef VALGRIND_CHECK */

    /* Clear the test data for this case */
    close(nbfd);
    close(bfd);
}

